﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Shop.Models;
using thousandClear.Util.Setting;
using static thousandClear.Areas.Shop.Controllers.BaseController;

namespace thousandClear.Areas.Shop.Controllers
{
    public class MarketController : BaseController
    {
        // GET: Shop/Market
        public ActionResult Index(string code, string state, string openid)
        {
            AppSetting setting = new AppSetting();
            MainViewModel list = new MainViewModel();
            try
            {
                UtilNS.Util.WriteLog("openid::" + openid);
                UtilNS.Util.WriteLog("code::" + code);

                if (setting.isNeedGetWX)
                {
                    if (string.IsNullOrEmpty(openid))
                    {
                        openid = GetOpenId(code).openid;

                        if (string.IsNullOrEmpty(openid))
                        {
                            Response.Redirect(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxed78d8fdb0030e29&redirect_uri=http%3a%2f%2f{0}%2fshop%2fMarket&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect", setting.AppDomainName.Replace("http://", "")));
                        }
                    }
                }
                else
                {
                    openid = setting.TestingOpenid;
                }

               

                UtilNS.Util.WriteLog("openidagain::" + openid);
                UtilNS.Util.WriteLog("codeagain::" + code);

                using (var db = new qianjingEntities())
                {
                    var cust = db.O_Customer.FirstOrDefault(a => a.ocOpenid.Trim() == openid);
                    list.TotalPoint = cust.ocGrades == null ? 0.00m : cust.ocGrades.Value;

                    var goodslist = db.D_ShopGood.OrderBy(gg => gg.SeqNum).Where(sa => sa.State == 1 && sa.Putaway == 1).ToList();

                    List<D_ShopGood> tempGood = new List<D_ShopGood>();
                    if (goodslist.Count > 0)
                    {
                        foreach (var good in goodslist)
                        {
                            var shopgood = new D_ShopGood();
                            shopgood.ImgUrl = string.Format("{0}/{1}/{2}", setting.AppDomainName, "statics/upload/good/image", good.ThumbUrl);
                            shopgood.Name = good.Name;
                            shopgood.Credit = good.Credit;
                            shopgood.DSGID = good.DSGID;
                            tempGood.Add(shopgood);
                        }
                    }
                    list.Goods = tempGood;

                    UtilNS.Util.WriteLog("正确第一步::"+ tempGood.Count.ToString());

                    var pointrecord = db.O_Shop_Point.OrderByDescending(oj => oj.CreateDate).Where(uo => uo.ospOpenID.Trim() == openid.Trim()).ToList();

                    List<D_Point> parm = new List<D_Point>();
                    if (pointrecord.Count > 0)
                    {
                        foreach (var poin in pointrecord)
                        {
                            var spoint = new D_Point();
                            spoint.UsePoint = poin.ospPoint.Value;
                            spoint.TimeStamp = poin.CreateDate.ToString();
                            parm.Add(spoint);
                        }
                    }
                    list.Points = parm;

                    UtilNS.Util.WriteLog("正确第二步::"+ parm.Count.ToString());

                    List<D_Order> order = new List<D_Order>();
                    var orderlist = db.O_Order_Info.OrderByDescending(cc => cc.CreatedTime).Where(op => op.OpenId == openid && op.Status > 0).ToList();
                    if (orderlist.Count > 0)
                    {
                        foreach (var poin in orderlist)
                        {
                            var listItem = db.D_Order_list_info.OrderByDescending(it => it.CreatedTime).Where(oi => oi.Id == poin.Id).ToList();
                            foreach (var detail in listItem)
                            {
                                D_Order dor = new D_Order();
                                dor.Amount = (detail.Amount * detail.Quantity).ToString();
                                dor.GoodImgUrl = string.Format("{0}/{1}/{2}", setting.AppDomainName, "statics/upload/good/image", detail.GoodImgUrl);
                                dor.GoodName = detail.GoodName;
                                dor.OrderId = poin.Id.ToString();
                                order.Add(dor);
                            }
                        }
                    }
                    list.Orders = order;
                    list.OpenId = openid;

                    UtilNS.Util.WriteLog("正确第三步::"+order.Count.ToString());
                }
                ViewBag.OpenId = openid;
                UtilNS.Util.WriteLog("正确::" + openid);
            }
            catch (Exception ex)
            {
                UtilNS.Util.WriteLog("ex::" + ex.Message);
                UtilNS.Util.WriteLog("innerex::" + ex.InnerException);
                Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx719afdb0aa0ebec4&redirect_uri=http%3a%2f%2fwww.qianjing-china.com%2fthousandclear%2fshop%Market&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect");
            }
            return View(list);
        }

    }
}